package com.nanhai.buddhist.college.module.study.controller.admin.studyevaluation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.study.controller.admin.studyevaluation.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyevaluation.StudyEvaluationDO;
import com.nanhai.buddhist.college.module.study.convert.studyevaluation.StudyEvaluationConvert;
import com.nanhai.buddhist.college.module.study.service.studyevaluation.StudyEvaluationService;

@Api(tags = "管理后台 - 学生评价")
@RestController
@RequestMapping("/study/evaluation")
@Validated
public class StudyEvaluationController {

    @Resource
    private StudyEvaluationService evaluationService;

    @PostMapping("/create")
    @ApiOperation("创建学生评价")
//    @PreAuthorize("@ss.hasPermission('study:evaluation:create')")
    public CommonResult<String> createEvaluation(@Valid @RequestBody StudyEvaluationCreateReqVO createReqVO) {
        return success(evaluationService.createEvaluation(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新学生评价")
//    @PreAuthorize("@ss.hasPermission('study:evaluation:update')")
    public CommonResult<Boolean> updateEvaluation(@Valid @RequestBody StudyEvaluationUpdateReqVO updateReqVO) {
        evaluationService.updateEvaluation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除学生评价")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation:delete')")
    public CommonResult<Boolean> deleteEvaluation(@RequestParam("id") String id) {
        evaluationService.deleteEvaluation(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得学生评价")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation:query')")
    public CommonResult<StudyEvaluationRespVO> getEvaluation(@RequestParam("id") String id) {
        StudyEvaluationDO evaluation = evaluationService.getEvaluation(id);
        return success(StudyEvaluationConvert.INSTANCE.convert(evaluation));
    }

    @GetMapping("/list-byids")
    @ApiOperation("获得学生评价列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation:query')")
    public CommonResult<List<StudyEvaluationRespVO>> getEvaluationList(@RequestParam("ids") Collection<String> ids) {
        List<StudyEvaluationDO> list = evaluationService.getEvaluationListByIds(ids);
        return success(StudyEvaluationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list")
    @ApiOperation("获得学生评价列表")
//    @PreAuthorize("@ss.hasPermission('study:evaluation:query')")
    public CommonResult<List<StudyEvaluationRespVO>> getEvaluationList(@Valid StudyEvaluationExportReqVO searchVo) {
        List<StudyEvaluationDO> list = evaluationService.getEvaluationList(searchVo);
        return success(StudyEvaluationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得学生评价分页")
//    @PreAuthorize("@ss.hasPermission('study:evaluation:query')")
    public CommonResult<PageResult<StudyEvaluationRespVO>> getEvaluationPage(@Valid StudyEvaluationPageReqVO pageVO) {
        PageResult<StudyEvaluationDO> pageResult = evaluationService.getEvaluationPage(pageVO);
        return success(StudyEvaluationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出学生评价 Excel")
//    @PreAuthorize("@ss.hasPermission('study:evaluation:export')")
    @OperateLog(type = EXPORT)
    public void exportEvaluationExcel(@Valid StudyEvaluationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudyEvaluationDO> list = evaluationService.getEvaluationList(exportReqVO);
        // 导出 Excel
        List<StudyEvaluationExcelVO> datas = StudyEvaluationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "学生评价.xls", "数据", StudyEvaluationExcelVO.class, datas);
    }

}
